<?php

class ControllerRouteLibExceptionYeti extends BaseRouteLibExceptionYeti {}

class ControllerRouteLibYeti extends BaseRouteLibYeti {
    public $listRoutes = Array();
    
    private $language = NULL, $listMethod = Array('get', 'post', 'put', 'delete', 'head', 'options');
    
    public function parseUrl($uri) { 
        
    }
    
    public function match($route, $controls) {
        if(!is_string($route)) {
            throw new ControllerRouteLibExceptionYeti('router_not_available');
        }
        
        if(!is_array($controls)) {
            $controls = $this->normalizeControls($controls);
        }
        
        if(count($controls) < 1) {
            throw new ControllerRouteLibExceptionYeti('router_not_available');
        }
        
        if(!in_array($controls['method_request'], $this->listMethod)) {
            $controls['method_request'] = 'get';
        }
        
        $this->listRoutes[] = Array($route, $controls);
    }
    
    public function ajax($route, $controls) {
        
    }
    
    public function xhr($route, $controls) {
        return $this->ajax($route, $controls);
    }
    
    public function userAgent($route, $controls) {
        
    }
    
    public function delete($route, $controls) {
        $controls['method_request'] = __FUNCTION__;
        $this->match($route, $controls);
    }
    
    public function get($route, $controls) {
        $controls['method_request'] = __FUNCTION__;
        $this->match($route, $controls);
    }
    
    public function head($route, $controls) {
        $controls['method_request'] = __FUNCTION__;
        $this->match($route, $controls);
    }
    
    public function options($route, $controls) {
        $controls['method_request'] = __FUNCTION__;
        $this->match($route, $controls);
    }
    
    public function post($route, $controls) {
        $controls['method_request'] = __FUNCTION__;
        $this->match($route, $controls);
    }
    
    public function put($route, $controls) {
        $controls['method_request'] = __FUNCTION__;
        $this->match($route, $controls);
    }
    
    
    private function normalizeControls($control) {
        if(!is_string($control) || stripos($control, '#') < 1) {
            return Array();
        }
        
        $split = explode('#', $control);
        $path  = YETI_PATH_CONTROLLER;
        $normalize = Array();
        
        if(count($split) > 1 AND Yeti::config('route.enable_package_in_simple_routes')) {
            if(is_dir($path . $split[0] . DIRECTORY_SEPARATOR)) {
                $path .= $split[0] . DIRECTORY_SEPARATOR;
                $normalize['package'] = array_shift($split);
                
                if(count($split) > 1 AND is_dir($path . $split[0] . DIRECTORY_SEPARATOR)) {
                    $path .= $split[0] . DIRECTORY_SEPARATOR;
                    $normalize['subpackage'] = array_shift($split);
                }
            }
        }
        
        if(!is_file($path . $split[0] . YETI_EXTENSION_FILE)) {
            throw new ControllerRouteLibExceptionYeti('router_not_found');
        }

        $path .= $split[0] . YETI_EXTENSION_FILE;
        $normalize['controller'] = array_shift($split);
        
        if(!empty($split)) {
            $normalize['method'] = array_shift($split);
            
            if(!empty($split)) {
                $normalize['param'] = $split;
            }
        }
        
        return $normalize;
    }
}
